package db_server_mysql

import (
	"PackingScanApp/db_mysql_server/base"
	"PackingScanApp/models"
)

//插入错误日志
func InsertErrLog(err_log *models.ErrLog) (int64, error) {
	sql := "INSERT INTO t_errlog (errmsg,date_time) VALUES (?,?)"
	ids := []interface{}{
		err_log.Errmsg,
		err_log.Date_time,
	}
	if id, err1 := base.Run_exec_insert(sql, ids); err1 != nil {
		return 0, err1
	} else {
		return id, nil
	}
}

//查询打印数据t_printdata
func SelectPrintData(printdata *models.PrintData) error {
	sql := "SELECT * FROM t_printdata"
	err := base.Run_selectRow(printdata, sql)
	return err
}

//根据时间查询当日生产数量
func SelectTodaySum(today_sum *models.Today_sum, time string) error {
	sql := "SELECT * FROM t_today_sum where time = ?"
	err := base.Run_selectRow(today_sum, sql, time)
	return err
}

//插入当日生产数量
func InsertTodaySum(today_sum *models.Today_sum) (int64, error) {
	sql := "INSERT INTO t_today_sum (num_sum,time) VALUES (?,?)"
	ids := []interface{}{
		today_sum.Num_sum,
		today_sum.Time,
	}
	if id, err1 := base.Run_exec_insert(sql, ids); err1 != nil {
		return 0, err1
	} else {
		return id, nil
	}
}

//更新当前生产数量
func UpdateTodaySum_Num(time string, num_sum int) error {
	sql := "UPDATE t_today_sum SET num_sum = num_sum+1 where time=? and num_sum=?"
	ids := []interface{}{time, num_sum}
	_, err1 := base.Run_exec(sql, ids)

	return err1
}

//取得COM口信息
func SelectCOMInfo(cominfos *[]models.ComInfo) error {
	sql := "SELECT * FROM t_cominfo"
	err := base.Run_selectRows(cominfos, sql)
	return err
}

//查询当前包装状态
func SelectNow_packaging_status(now_packaging_status *models.Now_packaging_status) error {
	sql := "SELECT * FROM t_now_packaging_status"
	err := base.Run_selectRow(now_packaging_status, sql)
	return err
}

//更新当前包装状态
func UpdateNowPackagingStatus(now_packaging_status *models.Now_packaging_status) error {
	sql := "UPDATE t_now_packaging_status SET packid = ?,packnum = ?,fbillno = ?,fnumber = ?,fname = ?,sum_packnum = ?"
	ids := []interface{}{
		now_packaging_status.Packid,
		now_packaging_status.Packnum,
		now_packaging_status.Fbillno,
		now_packaging_status.Fnumber,
		now_packaging_status.Sum_packnum,
		now_packaging_status.Fname,
	}
	_, err1 := base.Run_exec(sql, ids)

	return err1
}

//更新当前包装状态
func ResetNowPackagingStatus_D(now_packaging_status *models.Now_packaging_status, paking_id int64) error {
	ids := []interface{}{paking_id}
	ids1 := []interface{}{paking_id}
	ids2 := []interface{}{
		now_packaging_status.Packid,
		now_packaging_status.Packnum,
		now_packaging_status.Fbillno,
		now_packaging_status.Fnumber,
		now_packaging_status.Sum_packnum,
		now_packaging_status.Fname,
	}
	drS := []models.DatabaseTransaction{
		models.DatabaseTransaction{"UPDATE t_barcodepacking SET isdone=1 WHERE id=?", ids},
		models.DatabaseTransaction{"UPDATE t_barcodepackinglist SET isdone=1 WHERE packid =?", ids1},
		models.DatabaseTransaction{"UPDATE t_now_packaging_status SET packid = ?,packnum = ?,fbillno = ?,fnumber = ?,fname = ?,sum_packnum = ?", ids2},
	}

	return base.Run_exec_database_transaction(drS)
}

//清除正在包装的数据
func CleanNowPackagingStatus_D(now_packaging_status *models.Now_packaging_status, paking_id int) error {
	ids := []interface{}{paking_id}
	ids1 := []interface{}{paking_id}
	ids2 := []interface{}{
		now_packaging_status.Packid,
		now_packaging_status.Packnum,
		now_packaging_status.Fbillno,
		now_packaging_status.Fnumber,
		now_packaging_status.Sum_packnum,
		now_packaging_status.Fname,
	}
	drS := []models.DatabaseTransaction{
		models.DatabaseTransaction{"DELETE FROM t_barcodepacking WHERE id=?", ids},
		models.DatabaseTransaction{"DELETE FROM t_barcodepackinglist WHERE packid =?", ids1},
		models.DatabaseTransaction{"UPDATE t_now_packaging_status SET packid = ?,packnum = ?,fbillno = ?,fnumber = ?,fname = ?,sum_packnum = ?", ids2},
	}

	return base.Run_exec_database_transaction(drS)
}

//更新更新当前包装状态 fbillni和fnumber
func UpdateNowPackagingStatus_fbillno_fnumber_sumPacknum(iCOM_Data *models.ICOM_Data) error {
	sql := "UPDATE t_now_packaging_status SET fbillno = ?,fnumber = ?,fname = ?,sum_packnum = ?"
	ids := []interface{}{iCOM_Data.Fbillno,
		iCOM_Data.Fnumber,
		iCOM_Data.Fname,
		iCOM_Data.Sumnumber}
	_, err1 := base.Run_exec(sql, ids)

	return err1
}

//更新更新当前包装状态 包装id和包装数量,packnum为当前包装数量
func UpdateNowPackagingStatus_packid_packnum(packid int64, packnum int) error {
	packnum_plus := packnum + 1
	sql := "UPDATE t_now_packaging_status SET packid = ?,packnum = ? where packnum=?"
	ids := []interface{}{packid, packnum_plus, packnum}
	_, err1 := base.Run_exec(sql, ids)

	return err1
}

//查询当前正在包装的产品信息
func SelectNow_packing_data(now_packing_data *models.Now_packing_data) error {
	sql := "SELECT * FROM t_now_packing_data"
	err := base.Run_selectRow(now_packing_data, sql)
	return err
}

//更新当前正在包装的产品信息
func UpdateNow_packing_data(now_packing_data *models.Now_packing_data) error {
	ids := []interface{}{}
	ids1 := []interface{}{
		now_packing_data.P_type,
		now_packing_data.P_name,
		now_packing_data.Inputuser,
		now_packing_data.Line_mark,
		now_packing_data.Line_mark_note,
	}
	drS := []models.DatabaseTransaction{
		models.DatabaseTransaction{"DELETE FROM t_now_packing_data", ids},
		models.DatabaseTransaction{"INSERT INTO t_now_packing_data (p_type,p_name,Inputuser,line_mark,line_mark_note) VALUES (?,?,?,?,?)", ids1},
	}

	return base.Run_exec_database_transaction(drS)
}

//查询当前包装箱是否已经打印
func SelectBarcodepacking_isprint(barcodepacking *models.Barcodepacking, packid int64) error {
	sql := "SELECT * FROM t_barcodepacking where isprint=0 and id=?"
	err := base.Run_selectRow(barcodepacking, sql, packid)
	return err
}

//查询包装箱信息
func SelectBarcodepacking(barcodepacking *models.Barcodepacking, packNo string) error {
	sql := "SELECT * FROM t_barcodepacking where packNo=?"
	err := base.Run_selectRow(barcodepacking, sql, packNo)
	return err
}

//查询已打印未上传的包装箱信息
func SelectBarcodepacking_list(barcodepackings *[]models.Barcodepacking) error {
	sql := "SELECT * FROM t_barcodepacking where isdone=1 and isupdate=0"
	err := base.Run_selectRows(barcodepackings, sql)
	return err
}

//插入包装箱信息
func InsertBarcodepacking(barcodepacking *models.Barcodepacking) (int64, error) {
	sql := "INSERT INTO t_barcodepacking (packno,inputtime,inputuser,fbillno,fnumber) VALUES (?,?,?,?,?)"
	ids := []interface{}{
		barcodepacking.Packno,
		barcodepacking.Inputtime,
		barcodepacking.Inputuser,
		barcodepacking.Fbillno,
		barcodepacking.Fnumber,
	}
	if id, err1 := base.Run_exec_insert(sql, ids); err1 != nil {
		return 0, err1
	} else {
		return id, nil
	}
}

//更新打印时间，及打印状态 id 为 packid
func UpdateBarcodepacking_printtime(printtime string, id int64) error {
	sql := "UPDATE t_barcodepacking SET printtime = ?,isprint=1 where id=?"
	ids := []interface{}{printtime, id}
	_, err1 := base.Run_exec(sql, ids)

	return err1
}

//更新上传状态
func UpdateBarcodepacking_isupdate(id int64) error {
	sql := "UPDATE t_barcodepacking SET isupdate=1 where id=?"
	ids := []interface{}{id}
	_, err1 := base.Run_exec(sql, ids)

	return err1
}

//删除已经上传的数据;时间
func DeleteBarcodepacking_isupdate(inputtime string) error {
	sql := "DELETE FROM t_barcodepacking WHERE isupdate=1 and inputtime<?"
	ids := []interface{}{inputtime}
	_, err1 := base.Run_exec(sql, ids)

	return err1
}

//---t_barcodepackinglist
//查询未上传的包装箱列表
func SelectBarcodepackinglist_List(barcodepackinglists *[]models.Barcodepackinglist) error {
	sql := "SELECT * FROM t_barcodepackinglist where isdone=1 and isupdate=0"
	err := base.Run_selectRows(barcodepackinglists, sql)
	return err
}

//根据packid查询包装箱清单
func SelectBarcodepackinglist_packid(barcodepackinglists *[]models.Barcodepackinglist, packid int64) error {
	sql := "SELECT * FROM t_barcodepackinglist where packid=?"
	err := base.Run_selectRows(barcodepackinglists, sql, packid)
	return err
}

//查询包装箱清单
func SelectBarcodepackinglist_barcode(barcodepackinglist *models.Barcodepackinglist, barcode string) error {
	sql := "SELECT * FROM t_barcodepackinglist where barcode=?"
	err := base.Run_selectRow(barcodepackinglist, sql, barcode)
	return err
}

//更新包装箱清单上传状态
func UpdateBarcodepackinglist_isupdate(id int64) error {
	sql := "UPDATE t_barcodepackinglist SET isupdate=1 where id=?"
	ids := []interface{}{id}
	_, err1 := base.Run_exec(sql, ids)

	return err1
}

//删除包装箱清单
func DeleteBarcodepackinglist_isupdate(inputtime string) error {
	sql := "DELETE FROM t_barcodepackinglist WHERE isupdate=1 and inputtime<?"
	ids := []interface{}{inputtime}
	_, err1 := base.Run_exec(sql, ids)

	return err1
}

//插入包装箱清单
func InsertBarcodepackinglist(barcodepackinglist *models.Barcodepackinglist) error {
	sql := "INSERT INTO t_barcodepackinglist (packid,barcode,inputtime) VALUES (?,?,?)"
	ids := []interface{}{
		barcodepackinglist.Packid,
		barcodepackinglist.Barcode,
		barcodepackinglist.Inputtime,
	}
	_, err1 := base.Run_exec_insert(sql, ids)

	return err1
}

//更新打印时间，及打印状态 id 为 packid
func UpdatePrint_s(printtime string, packid int64, printdata *models.PrintData, today_sum *models.Today_sum) error {
	ids := []interface{}{printtime, packid}
	ids1 := []interface{}{}
	ids2 := []interface{}{
		printdata.Part_number,
		printdata.Part_description,
		printdata.Lot_number,
		printdata.Batch_number,
		printdata.Quantity,
		printdata.Date_s,
		printdata.Code_base64_str,
		printdata.Print_type,
		printdata.Address_s,
	}
	ids3 := []interface{}{
		today_sum.Time,
		today_sum.Num_sum,
	}
	drS := []models.DatabaseTransaction{
		models.DatabaseTransaction{"UPDATE t_barcodepacking SET printtime = ?,isprint=1 where id=?", ids},
		models.DatabaseTransaction{"DELETE FROM t_printdata", ids1},
		models.DatabaseTransaction{"INSERT INTO t_printdata (part_number,part_description,lot_number,batch_number,quantity,date_s,code_base64_str,print_type,address_s) VALUES (?,?,?,?,?,?,?,?,?)", ids2},
		models.DatabaseTransaction{"UPDATE t_today_sum SET num_sum = num_sum+1 where time=? and num_sum=?", ids3},
	}

	return base.Run_exec_database_transaction(drS)
}

//-------------------------------------------

//包装条码更新
func UpdateOtherDataTransaction(barcodepackinglist *models.Barcodepackinglist, packnum int) error {
	ids := []interface{}{
		barcodepackinglist.Packid,
		barcodepackinglist.Barcode,
		barcodepackinglist.Inputtime,
	}
	ids1 := []interface{}{
		barcodepackinglist.Packid,
		packnum + 1,
		packnum,
	}
	drS := []models.DatabaseTransaction{
		models.DatabaseTransaction{"INSERT INTO t_barcodepackinglist (packid,barcode,inputtime) VALUES (?,?,?)", ids},
		models.DatabaseTransaction{"UPDATE t_now_packaging_status SET packid = ?,packnum = ? where packnum=?", ids1},
	}

	return base.Run_exec_database_transaction(drS)
}

//-------------40服务器server-------------
//查询包装人员用户
func SelectUser(packagingUser *models.PackagingUser, userid, userpasswordmd5 string) error {
	sql := "SELECT * FROM t_packing_user where userid=? and userpasswordmd5=?"
	err := base.Run_selectRow_S(packagingUser, sql, userid, userpasswordmd5)
	return err
}

//插入包装人员
func InsertUser(packagingUser *models.PackagingUser) error {
	sql := "INSERT INTO t_user (userid,userpasswordmd5,username) VALUES (?,?,?)"
	ids := []interface{}{
		packagingUser.Userid,
		packagingUser.Userpasswordmd5,
		packagingUser.Username,
	}
	_, err1 := base.Run_exec_insert_S(sql, ids)
	return err1
}

//删除包装人员用户
func DeleteUser(id int64) error {
	sql := "DELETE FROM t_user where id = ?"
	_, err1 := base.Run_exec_S(sql, id)
	return err1
}

//查询老厂LBB产品
func SelectProduct_Count(productNo string) (int, error) {
	count_num := 0
	sql := "Select count(*) as count_num from product where productNo = ?"
	err := base.Run_selectRow_S(count_num, sql, productNo)
	return count_num, err
}

//--------------
//插入包装箱信息
func InsertBarcodepacking_S40(barcodepacking *models.Barcodepacking) (int64, error) {
	sql := "INSERT INTO t_barcodepacking (packno,inputtime,inputuser,fbillno,fnumber) VALUES (?,?,?,?,?)"
	ids := []interface{}{
		barcodepacking.Packno,
		barcodepacking.Inputtime,
		barcodepacking.Inputuser,
		barcodepacking.Fbillno,
		barcodepacking.Fnumber,
	}
	if id, err1 := base.Run_exec_insert(sql, ids); err1 != nil {
		return 0, err1
	} else {
		return id, nil
	}
}

//插入包装箱清单
func InsertBarcodepackinglist_S40(barcodepackinglist *models.Barcodepackinglist) error {
	sql := "INSERT INTO t_barcodepackinglist (packid,barcode,inputtime) VALUES (?,?,?)"
	ids := []interface{}{
		barcodepackinglist.Packid,
		barcodepackinglist.Barcode,
		barcodepackinglist.Inputtime,
	}
	_, err1 := base.Run_exec_insert_S(sql, ids)

	return err1
}
